/*
 * @Autor: dc
 * @Date: 2022-03-29 17:02:37
 * @LastEditors: dc
 * @LastEditTime: 2022-03-30 17:11:41
 * @Description: 
    * 该文件是用于创建一个为Count组件服务的reducer,reducer的本质就是一个函数;
    * reducer函数会接收到两个参数,分别为:之前的状态(preState),动作对象(action)
 */
import {INCREMENT,DECREMENT} from '../constant'


const initState = 0 //初始化状态
function countReducer(preState = initState, action) {
    // 从action对象中获取:type\data
    const { type, data } = action;

    // 根据type决定如何加工数据
    switch (type) {
        // 加
        case INCREMENT:
            return preState + data * 1
        // 减
        case DECREMENT:
            return preState - data * 1

        default:
            return preState;
    }
}

// 暴露count_reducer
export default countReducer
